package com.step.scene.camera;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;

import com.step.Step;
import com.step.Step.Dimension;
import com.step.math.Vector;

/**
 * 第三人称视角相机
 * @author heop
 *
 */
public class LookAtCamera extends Camera{
	final Vector position;
	final Vector up;
	final Vector lookAt;
	float fieldOfView;
	float aspectRatio;
	float near;
	float far;
	
	public LookAtCamera(){
		this(67, Step.aspectRatio, 1, 100);
	}
	/**
	 * 创建第三人称视角相机
	 * @param fieldOfView 视野角度
	 * @param aspectRatio 视窗长宽比
	 * @param near 近点裁剪平面
	 * @param far 远点裁剪平面
	 */
	public LookAtCamera(float fieldOfView, float aspectRatio, float near, float far) {
		super(CameraType.LOOKAT_CAMERA);
		this.fieldOfView = fieldOfView;
		this.aspectRatio = aspectRatio;
		this.near = near;
		this.far = far;
		position = new Vector(Dimension.d3d);
		up = new Vector(0, 1, 0);
		lookAt = new Vector(0,0,-1);
	}

	@Override
	public void updateFrustum(GL10 gl) {
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluPerspective(gl, fieldOfView, aspectRatio, near, far);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		
		GLU.gluLookAt(gl, 
				position.getX(), position.getY(), position.getZ(), 
				lookAt.getX(), lookAt.getY(),	lookAt.getZ(), 
				up.getX(), up.getY(), up.getZ());
	}

	public Vector getPosition() {
		return position;
	}

	public Vector getUp() {
		return up;
	}

	public Vector getLookAt() {
		return lookAt;
	}
}
